<template>
  <a-card :bordered="false">
    <a-row >
      <a-col :span="6" style="padding-right:12px;height:100%">
        <DeptList ref="DeptList" :key="currentKey" @listenTochildEvent="showMessageFromChild"></DeptList>
      </a-col>
      <a-col :span="18">
        <div class="table-operator">
          <div class="icons-list">
            <a-icon type="form" @click="hanldleAdd()" style="color:#1890ff;" title="新建" />
            <a-icon type="delete" style="color:red;" title="删除" @click="handleDelete(selectedRowKeys)" :disabled="!hasSelected()"/>
          </div>
        </div>

        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="6" :sm="24">
                <a-form-item label="关键字">
                  <a-input v-model="queryParam.keyword" placeholder />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-button type="primary" @click="getDataList">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>

        <a-table
          ref="table"
          :columns="columns"
          :rowKey="row => row.Code"
          :dataSource="data"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :bordered="true"
          size="small"
          :scroll="{ y: height }"
        >
          <span slot="action" slot-scope="text, record">
            <template>
              <template v-if="hasPerm('Base_User.Edit')">
                <a @click="handleEdit(record.Code)">编辑</a>
                <a-divider type="vertical" />
              </template>
              <a v-if="hasPerm('Base_User.Delete')" @click="handleDelete([record.Code])">删除</a>
            </template>
          </span>
        </a-table>

      </a-col>
    </a-row>

    <edit-form ref="editForm" :afterSubmit="getDataList"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import DeptList from './Dept'
import moment from 'moment'
// eslint-disable-next-line no-unused-vars
const formatterTime = (val) => {
  return val ? moment(val).format('YYYY-MM-DD') : ''
}

const columns = [
  { title: '用户名', dataIndex: 'Code', width: '10%' },
  { title: '姓名', dataIndex: 'UseName', width: '10%' },
  { title: '性别', dataIndex: 'Sex', width: '5%' },
  { title: '岗位', dataIndex: 'Post', width: '10%' },
  { title: '类型', dataIndex: 'UserType', width: '10%' },
  { title: '组别', dataIndex: 'LineGroup', width: '20%' },
  // eslint-disable-next-line no-undef
  { title: '离职日期', dataIndex: 'DimissionDate', width: '10%', render: formatterTime },
  { title: '状态', dataIndex: 'State', width: '5%' },
  { title: '操作', dataIndex: 'action', width: '100px', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm,
    DeptList
  },
  mounted () {
    this.getDataList()
  },
  props: {
    // eslint-disable-next-line vue/require-default-prop
    rowData: {
      type: Object
    }
  },
  watch: {
    rowData () {
      this.temp = this.rowData
    }
  },
  data () {
    return {
      data: [],
      currentKey: '',
      temp: this.rowData,
      height: window.innerHeight - 310,
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'Code', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      visible: false,
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/Aps/Aps_Personnel/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Code',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    showMessageFromChild (data) {
      this.queryParam.keyword = data.text
      this.queryParam.orgid = data.value
      this.getDataList()
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.$refs.editForm.openForm()
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id)
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.submitDelete(ids, resolve, reject)
          }).catch(() => console.log('Oops errors!'))
        }
      })
    },
    submitDelete (ids, resolve, reject) {
      this.$http.post('/Aps/Aps_Personnel/DeleteData', ids).then(resJson => {
        resolve()

        if (resJson.Success) {
          this.$message.success('操作成功!')

          this.getDataList()
        } else {
          this.$message.error(resJson.Msg)
        }
      })
    }
  }
}
</script>
